/**
 * @Description: 路由权限
 * @Author bianpengfei
 * @create 2022/11/24 20:33
 */
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

NProgress.configure({ showSpinner: false })

/**
 * 白名单
 * @type {string[]}
 */
// const whiteList = ['/Authorization', '/Login'].concat(publicRoutes.filter((v) => v?.path !== '/').map((v) => v.path))

/**
 * 公共路由
 * @type {string[]}
 */
const publicPaths = ['/Error/Page401'].map((v) => {
  const matches = v.match(/(.*?\/):(\w+)/)
  return matches?.length ? new RegExp(`^${matches[1]}`) : new RegExp(`^${v}$`)
})

/**
 * 路由钩子鉴权
 */
export const authRouterMiddleware = function () {
  // const appStore = useAppStore()

  /**
   * 路由鉴权
   */
  this.beforeEach(async (to, from, next) => {
    // 在非乾坤中 显示加载进度条
    NProgress.start()

    // 公共路由，不需要权限，放行
    if (publicPaths.some((v) => !!v?.test(to.path)) || to.meta.auth === false) return next()

    // 最终放行
    next()
  })

  // eslint-disable-next-line no-unused-vars
  this.afterEach((to, from, next) => {
    // 在非乾坤中 关闭加载进度条
    NProgress.done()
  })
}
